package com.cap.bts.crmdp.webservice.dc;

import java.util.Calendar;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import com.cap.bts.crmdp.dealer.mars.domain.DataConversionLog;
import com.cap.bts.crmdp.dealer.mars.service.IDataConversionLogService;
import com.cap.bts.framework.codemapping.service.ICodeMapping;
import com.cap.bts.framework.common.util.ApplicationContextUtil;

public class DataConversionLoadDataThread extends Thread{
	private static Logger logger = Logger.getLogger(DataConversionQueue.class);
	@Override
	public void run(){
		logger.info("start load conversion data thread.");
		while(true){
			try {
				Thread.sleep(300000);
			} catch (Exception e) {
				logger.error("", e);
			}
			try {
				Calendar calendar = Calendar.getInstance();
				int hour = calendar.get(Calendar.HOUR_OF_DAY);
				ICodeMapping codeMappingService = (ICodeMapping) ApplicationContextUtil.getBean("codeMappingImpl");
				String runFlag = codeMappingService.getCodeValueByCode("DataConvertIsRun", "DataConvertIsRun");
				
				if(StringUtils.equalsIgnoreCase(runFlag, "1") || hour >= 19 || hour < 7){
					IDataConversionLogService dataConversionLogServiceImpl = (IDataConversionLogService) ApplicationContextUtil.getBean("dataConversionLogServiceImpl");
					List<DataConversionLog> dataList = dataConversionLogServiceImpl.getPendingData();
					for (DataConversionLog dataConversionLog : dataList) {
						DataConversionQueue.insertData(dataConversionLog);
					}
				}
			} catch (Exception e) {
				logger.error("", e);
			}
		}
	}
}
